package com.example.administrator.androidros.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.androidros.R;
import com.example.administrator.androidros.lrecyclerview.ListBaseAdapter;
import com.example.administrator.androidros.lrecyclerview.SuperViewHolder;
import com.example.administrator.androidros.lrecyclerview.SwipeMenuView;
import com.hhc.netty.DeviceModel;

/**
 * Created by Administrator on 2018/4/17.
 */

public class SwipeMenuAdapter extends ListBaseAdapter<DeviceModel> {
    public SwipeMenuAdapter(Context context) {
        super(context);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_scene_item_scene_detail;
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, final int position) {
        View contentView = holder.getView(R.id.activity_scene_item_scene_detail_item);
        Button btnDelete = holder.getView(R.id.btnDelete);
        ImageView activity_scene_item_scene_detail_img_icon = holder.getView(R.id.activity_scene_item_scene_detail_img_icon);
        TextView activity_scene_item_scene_detail_tv_name = holder.getView(R.id.activity_scene_item_scene_detail_tv_name);
        TextView activity_scene_item_scene_detail_tv_status = holder.getView(R.id.activity_scene_item_scene_detail_tv_status);
        Switch activity_scene_item_scene_detail_switch = holder.getView(R.id.activity_scene_item_scene_detail_switch);

        //这句话关掉IOS阻塞式交互效果 并依次打开左滑右滑
        ((SwipeMenuView) holder.itemView).setIos(false).setLeftSwipe(true);

        if (mDataList.get(position).getApplianceType().equals("10")) {
            activity_scene_item_scene_detail_img_icon.setImageResource(R.mipmap.ic_launcher);
        } else if (mDataList.get(position).getApplianceType().equals("20")) {
            activity_scene_item_scene_detail_img_icon.setImageResource(R.mipmap.ic_launcher);
        } else if (mDataList.get(position).getApplianceType().equals("30")) {
            activity_scene_item_scene_detail_img_icon.setImageResource(R.mipmap.ic_launcher);
        }
        activity_scene_item_scene_detail_tv_name.setText(mDataList.get(position).getApplianceName());
        // 设置设备在线状态 -> 10:在线 20:离线
        if (mDataList.get(position).getOnlineStatus().equals("10")) {
            activity_scene_item_scene_detail_tv_status.setText("在线");
            activity_scene_item_scene_detail_tv_status.setTextColor(ContextCompat.getColor(mContext, R.color.lawn_green));
        } else if (mDataList.get(position).getOnlineStatus().equals("20")) {
            activity_scene_item_scene_detail_tv_status.setText("离线");
            activity_scene_item_scene_detail_tv_status.setTextColor(ContextCompat.getColor(mContext, R.color.red));

            //同时开关按钮不能点击
            activity_scene_item_scene_detail_switch.setClickable(false);
        }

        // 设置设备状态 -> 10:开 20:关
        if (mDataList.get(position).getStatus().equals("10")) {
            activity_scene_item_scene_detail_switch.setChecked(true);
        } else if (mDataList.get(position).getStatus().equals("20")) {
            activity_scene_item_scene_detail_switch.setChecked(false);
        }

        activity_scene_item_scene_detail_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Toast.makeText(mContext, "打开", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(mContext, "关闭", Toast.LENGTH_SHORT).show();
                }
            }
        });

        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mOnSwipeListener) {
                    //如果删除时，不使用mAdapter.notifyItemRemoved(pos)，则删除没有动画效果，
                    //且如果想让侧滑菜单同时关闭，需要同时调用 ((CstSwipeDelMenu) holder.itemView).quickClose();
                    //((CstSwipeDelMenu) holder.itemView).quickClose();
                    mOnSwipeListener.onDel(position);
                }
            }
        });

        //注意事项，设置item点击，不能对整个holder.itemView设置咯，只能对第一个子View，即原来的content设置，这算是局限性吧。
        contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("hhc", "onClick: " + position);
            }
        });
    }

    /**
     * 和Activity通信的接口
     */
    public interface onSwipeListener {
        void onDel(int pos);
    }

    private onSwipeListener mOnSwipeListener;

    public void setOnDelListener(onSwipeListener mOnDelListener) {
        this.mOnSwipeListener = mOnDelListener;
    }
}
